package com.ctos.ojbackendmodel.vo;

import cn.hutool.json.JSONUtil;

import com.ctos.ojbackendmodel.dto.questionsubmit.JudgeInfo;
import com.ctos.ojbackendmodel.entity.QuestionSubmit;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;

/**
 * 题目提交封装类
 * @TableName question_submit
 */

@Data
public class QuestionSubmitVO implements Serializable {
    /**
     * id
     */
    private Long id;

    /**
     * 编程语言
     */
    private String language;

    /**
     * 用户代码
     */
    private String code;

    /**
     * 判题信息（json 对象）
     */
    private JudgeInfo judgeInfo;

    /**
     * 判题状态（0 - 待判题、1 - 判题中、2 - 成功、3 - 失败）
     */
    private Integer status;

    /**
     * 题目 id
     */
    private Long questionId;

    /**
     * 创建用户 id
     */
    private Long userId;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    //提交题目 用户信息
    private UserVO userVO;

    //提交题目 题目信息
    private QuestionVO questionVO;

    private static final long serialVersionUID = 1L;

    /**
     * 包装类转对象
     *
     * @param questionSubmitVo
     * @return
     */
    public static QuestionSubmit voToObj(QuestionSubmitVO questionSubmitVo) {
        if (questionSubmitVo == null) {
            return null;
        }
        QuestionSubmit questionSubmit = new QuestionSubmit();
        BeanUtils.copyProperties(questionSubmitVo, questionSubmit);
        JudgeInfo judgeInfo = questionSubmitVo.getJudgeInfo();
        if (judgeInfo != null){
            questionSubmit.setJudgeInfo(JSONUtil.toJsonStr(judgeInfo));
        }

        return questionSubmit;
    }

    /**
     * 对象转包装类
     *
     * @param questionSubmit
     * @return
     */
    public static QuestionSubmitVO objToVo(QuestionSubmit questionSubmit) {
        if (questionSubmit == null) {
            return null;
        }
        QuestionSubmitVO questionSubmitVo = new QuestionSubmitVO();
        BeanUtils.copyProperties(questionSubmit, questionSubmitVo);
        questionSubmitVo.setJudgeInfo((JSONUtil.toBean(questionSubmit.getJudgeInfo(), JudgeInfo.class)));

        return questionSubmitVo;
    }
}